package net.jeeshop.web.action.front.product;

import net.jeeshop.core.Services;
import net.jeeshop.services.front.catalog.CatalogService;
import net.jeeshop.services.front.catalog.bean.Catalog;
import net.jeeshop.web.action.front.FrontBaseController;
import net.jeeshop.web.util.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zuohao on 16/7/6.
 */
@Controller("frontCatalogAction")
@RequestMapping("catalog")
public class CatalogAction extends FrontBaseController<Catalog> {

    private static final long serialVersionUID = 1L;
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CatalogAction.class);

    @Autowired
    private CatalogService catalogService;

    @Override
    public Services<Catalog> getService() {
        return catalogService;
    }

    @RequestMapping("{catalogCode}")
    public String catalog(@PathVariable("catalogCode") String catalogCode) {
        return "forward:/product/productList?catalogCode=" + catalogCode;
    }

    @RequestMapping("attr/{attrID}")
    public String attr(@PathVariable("attrID") String attrID) {
        return "forward:/product/productList?attrID=" + attrID;
    }

    /**
     * 查询指定父类下的所有类型
     *
     * @param parentCode
     * @return
     */
    @RequestMapping("catalogListByParent")
    @ResponseBody
    public Map<String, Object> catalogListByParentCode(@RequestParam("parentCode") String parentCode) {
        List<Catalog> catalogList = catalogService.selectCatalogListByParentCode(parentCode);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", "000000");
        result.put("catalogList", catalogList);
        return result;
    }

    @RequestMapping("catalogListByPid")
    @ResponseBody
    public Map<Object, Object> cataLogListByParentPid(@RequestParam("pid") Integer pid) {
        Catalog catalog = new Catalog();
        catalog.setPid("" + pid);
        List<Catalog> catalogList = catalogService.selectList(catalog);
        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("status", "success");
        result.put("catalogList", catalogList);
        return result;
    }

    @RequestMapping("catalogListByParentAndType")
    @ResponseBody
    public Map<Object, Object> catalogListByParentAndType() {
        String local = RequestHolder.getRequest().getParameter("local");

        Map sqlMap = new HashMap();
        if (StringUtils.isBlank(local) || "null".equals(local)) {
            local = null;
        }
        sqlMap.put("local", local);
        Map resultMap = new HashMap();
        resultMap.put("status", "success");

        resultMap.put("cataList", catalogService.catalogListByParentAndType(sqlMap));
        return resultMap;
    }
}
